<?php
namespace Admin\Controller;
use Think\Controller;
class SpecController extends CommonController {


    public function _initialize() {
        $params = array();
        parent::_initialize($params);
        $this->assign("now_module" ,"goods");
    }


    //规格列表页面
    public function index() {
        //搜索条件
        $btn = I('get.btn');
        if (isset($btn)) {
            $shop_id = I('get.shop_id');
            if (!empty($shop_id)) $map['sp.shop_id'] = I('get.shop_id');
        }

        //查询
        $count = M('Spec sp')->where($map)->count();
        $page = set_page($count, C('PAGE_NUM'));
        $show = $page->show();

        $spec = M('Spec sp')->field('sp.*,s.shop_name')->join('LEFT JOIN __SHOP__ s ON sp.shop_id=s.id')
                            ->where($map)->limit($page->firstRow.','.$page->listRows)->order('sp.id DESC')
                            ->select();

        //所有的店铺
        $shop = M('shop')->order('id DESC')->select();
        $this->assign('shop_id', $shop_id);
        $this->assign('page',$show);
        $this->assign('shops', $shop);
        $this->assign('spec',$spec);
        $this->display();
    }


    //添加规格
    public function addSpec() {
        if(IS_POST) {
            $bool = M('spec')->add( array('s_name'=>I('post.s_name')) );
            $this->success("添加成功" ,U('Spec/index') ,1);
        } else {
            $this->display();
        }
    }



    //修改规格
    public function edit() {
        if (IS_POST) {
            $id = I('post.iid');
            $bool = M('spec')->where( array('id'=>$id) )->save( array('s_name'=>I('post.s_name')) );
            $this->ajaxReturn($bool);
        } else {
            $id = I('get.id');
            $spec = M('spec')->where( array('id'=>$id) )->find();
            $this->assign('spec',$spec);
            $this->display();
        }
    }



    //删除规格
    public function del() {
        $id = I('post.id');
        $bool = M('spec')->where( array('id'=>$id) )->delete();
        $this->ajaxReturn($bool);
    }






}